package com.love.testnetwork;

import com.love.testnetwork.entity.BaseResponse;
import com.love.testnetwork.entity.GuestureEntity;
import com.love.testnetwork.entity.UserEntity;

import java.io.File;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.Part;
import retrofit2.http.Query;
import retrofit2.http.QueryMap;
import retrofit2.http.Url;
import rx.Observable;

/**
 * Created by zf on 2016/11/25.
 */

public class HttpMethods implements ApiService {

    private static final int DEFAULT_TIME_OUT = 5;//超时时间 5s
    private static final int DEFAULT_READ_TIME_OUT = 10;
    private static Retrofit retrofit;

    private ApiService apiService;


    public HttpMethods() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.connectTimeout(DEFAULT_TIME_OUT, TimeUnit.SECONDS);
        builder.readTimeout(DEFAULT_READ_TIME_OUT, TimeUnit.SECONDS);


        File httpCacheDirectory = new File(MainApp.getAppContext().getCacheDir(), "kh_cache");
        int cacheSize = 10 * 1024 * 1024; // 10 MiB
        Cache cache = new Cache(httpCacheDirectory, cacheSize);
        builder.cache(cache);
        builder.addInterceptor(new LogInterceptor());


        retrofit = new Retrofit.Builder()
                .client(builder.build())
                .baseUrl("http://app1.ichezheng.com/CarcareService/")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        apiService = retrofit.create(ApiService.class);
    }


    public static <T> T createApi(Class<T> clz) {
        return (T) retrofit.create(clz);
    }

    @Override
    public Call<BaseResponse<UserEntity>> getUserInfo(@Query("userid") String userid, @Query("session") String session) {
        return apiService.getUserInfo(userid, session);
    }

    @Override
    public Call<GuestureEntity> getGuestureInfo(@QueryMap(encoded = true) Map<String, String> options) {
        return apiService.getGuestureInfo(options);
    }

    @Override
    public Call<ResponseBody> updateImage(@Url String url, @QueryMap(encoded = true) Map<String, String> map, @Part("fileName") String description, @Part("file\"; filename=\"image.jpg\"") RequestBody imgs) {
        return apiService.updateImage(url, map, description, imgs);
    }

    @Override
    public Observable<BaseResponse<UserEntity>> getInfo(@Query("userid") String userid, @Query("session") String session) {
        return apiService.getInfo(userid, session);
    }

    @Override
    public Observable<GuestureEntity> getGuesture(@Query("userid") String userid, @Query("session") String session, @Query("imei") String imei) {
        return apiService.getGuesture(userid, session, imei);
    }


    private static class SingletonHolder {
        private static final HttpMethods INSTANCE = new HttpMethods();
    }

    public static HttpMethods getInstance() {
        return SingletonHolder.INSTANCE;
    }

}
